package weixinkeji.dev.power.center.h;

import java.util.ArrayList;
import java.util.List;
import weixinkeji.dev.power.center.bean.SysUR;
import sys.wx.c.ATR;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.ann.power.head.GL;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.m.db.dao.mysql.Dao2;
import sys.wx.util.ToolService;
import weixinkeji.dev.power.center.so.URPSO;
import weixinkeji.dev.power.role.bean.SysRole;

/**
 *
 * @author IK
 */
@H("weixinkeji/dev/power/center/sysur/adu")
@GL
public class SysURADU {

    ATR a;                  //ATR是必须要的。它封装了web请求的相关信息
    URPSO so = new URPSO();

    public SysURADU(ATR obj) {     //这个有参构造函数，也是必须的。它进行附值
        this.a = obj;
    }

    @M("/add")//weixinkeji/dev/power/center/sysur/adu/add.
    public String a1() {

        String uID = a.request.getParameter("uID");
        String rIDs = a.request.getParameter("rID");

        if (ToolService.IsNullOREmpty(rIDs)) {
            a.out_OK_NO(Dao.executeUpdate(so.clearUserRoles(uID)));
            return null;
        }
//------------------------------------------------
        int roleCount = Dao.selectCount(SysRole.class, Where.putIN(Where.TJ_IGNORE_EMPTY_NULL, SysRole.class, "", "role_id", rIDs));
        String[] rids = rIDs.replace("'", "").split(",");
        if (roleCount != rids.length) {
            a.out_OK_NO(false, "角色ID出错。无法进行添加。");
            return null;
        }
//------------------------------------------------
        List<SysUR> lur = new ArrayList<>();
        SysUR obj;
        for (String rid : rids) {
            obj = new SysUR();
            obj.setUser_id(uID);
            obj.setRole_id(rid);
            lur.add(obj);
        }
//-----------------------------------------------

        a.out_OK_NO(Dao2.sql(so.clearUserRoles(uID), so.setUR(lur)));
        return null;
    }

}
